package com.blank.white.common.util;


import com.google.common.hash.Hashing;
import org.apache.commons.lang3.RandomStringUtils;

import java.math.BigDecimal;
import java.text.SimpleDateFormat;
import java.util.*;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class BaseUtil {
    public static final String PATTERN_DEFAULT_DATETIME = "yyyy-MM-dd HH:mm:ss";
    public static final String PATTERN_DEFAULT_DATE = "yyyy-MM-dd";
    private static final Pattern PATTERN_NUM = Pattern.compile("[0-9]+");
    private static final String[] CHINESE_NUM_CHAR = new String[]{"零", "一", "二", "三", "四", "五", "六", "七", "八", "九"};
    private static final String[] CHINESE_UNIT_CHAR = new String[]{"", "十", "百", "千"};

    private BaseUtil() {
    }

    public static boolean isNullOrEmpty(List<?> list) {
        return list == null || list.isEmpty();
    }

    public static boolean hasElements(List<?> list) {
        return !isNullOrEmpty(list);
    }

    public static boolean isNullOrEmpty(Object[] arr) {
        return arr == null || arr.length < 1;
    }

    public static boolean hasElements(Object[] arr) {
        return !isNullOrEmpty(arr);
    }

    public static boolean isNullOrEmpty(Set<?> set) {
        return set == null || set.isEmpty();
    }

    public static boolean hasElements(Set<?> set) {
        return !isNullOrEmpty(set);
    }

    public static boolean isNullOrEmpty(Map<?, ?> model) {
        return model == null || model.isEmpty();
    }

    public static boolean hasElements(Map<?, ?> model) {
        return !isNullOrEmpty(model);
    }

    public static boolean isNullOrEmpty(String str) {
        return str == null || str.trim().length() < 1;
    }

    public static boolean hasContent(String str) {
        return !isNullOrEmpty(str);
    }

    public static boolean isNullOrTrimEmpty(String str) {
        return str == null || str.trim().length() < 1;
    }

    public static String currentDate(String pattern) {
        return (new SimpleDateFormat(pattern)).format(new Date());
    }

    public static String genUUID() {
        return UUID.randomUUID().toString().toUpperCase().replace("-", "");
    }

    public static String currentDateTime() {
        return (new SimpleDateFormat("yyyy-MM-dd HH:mm:ss")).format(new Date());
    }

    public static String date2string(Date date, String format) {
        return date == null ? null : (new SimpleDateFormat(format)).format(date);
    }

    public static String getSimpleClassName(Class<?> clazz) {
        return clazz == null ? null : clazz.getSimpleName();
    }

    public static String getAllClassName(Class<?> clazz) {
        return clazz == null ? null : clazz.getName();
    }

    public static boolean isNumberic(String str) {
        if (isNullOrEmpty(str)) {
            return false;
        } else {
            Matcher isNum = PATTERN_NUM.matcher(str);
            return isNum.matches();
        }
    }

    public static <E> List<E> newList() {
        return new ArrayList();
    }

    public static <E> List<E> newList(int initCapacity) {
        return new ArrayList(initCapacity);
    }

    @SafeVarargs
    public static <E> List<E> newList(E... es) {
        if (es == null) {
            return null;
        } else {
            List<E> list = new ArrayList(es.length);
            Collections.addAll(list, es);
            return list;
        }
    }

    public static <K, V> Map<K, V> newMap() {
        return new HashMap();
    }

    public static <K, V> Map<K, V> newMap(int initCapacity) {
        return new HashMap(initCapacity);
    }

    public static <E> Set<E> newSet() {
        return new HashSet();
    }

    public static <E> Set<E> newSet(E... es) {
        return new HashSet(Arrays.asList(es));
    }

    public static boolean notIn(int a, int... iArr) {
        return !in(a, iArr);
    }

    public static boolean in(int a, int... iArr) {
        int[] var2 = iArr;
        int var3 = iArr.length;

        for(int var4 = 0; var4 < var3; ++var4) {
            int i = var2[var4];
            if (a == i) {
                return true;
            }
        }

        return false;
    }

    public static String discardSensitive(String str) {
        if (isNullOrEmpty(str)) {
            return "";
        } else {
            int len = str.length();
            switch(len) {
                case 1:
                    return "*";
                case 2:
                    return "**";
                default:
                    return str.charAt(0) + "****" + str.charAt(len - 1);
            }
        }
    }

    public static String discardTelSensitive(String str) {
        if (isNullOrEmpty(str)) {
            return "";
        } else {
            int len = str.length();
            return len < 11 ? "*****" : str.substring(0, 3) + "****" + str.substring(7, len);
        }
    }

    public static String safeMobile(String auth) {
        if (isNullOrEmpty(auth)) {
            return "";
        } else {
            int len = auth.length();
            return len < 11 ? "*******" : auth.substring(0, 3) + "****" + auth.substring(7, len);
        }
    }

    public static String bigDecimal2string(BigDecimal money, int scale) {
        if (money != null) {
            return money.setScale(scale, 4).toString();
        } else {
            StringBuilder sb = new StringBuilder();
            sb.append("0.");

            for(int i = 0; i < scale; ++i) {
                sb.append("0");
            }

            return sb.toString();
        }
    }

    public static Long default2long(String id, long defaultVal) {
        try {
            return Long.valueOf(id);
        } catch (Exception var4) {
            return defaultVal;
        }
    }

    public static String base64Decode(String str) {
        return new String(Base64.getDecoder().decode(str));
    }

    public static String deleteHttpPrefix(String url) {
        return !isNullOrTrimEmpty(url) && url.startsWith("http:") ? url.replaceFirst("http:", "") : url;
    }

    public static BigDecimal min(BigDecimal a, BigDecimal b) {
        return a.compareTo(b) < 0 ? a : b;
    }

    public static String join(String[] skuArr, String splitor) {
        if (isNullOrEmpty((Object[])skuArr)) {
            return null;
        } else {
            StringBuilder sb = new StringBuilder();
            String[] var3 = skuArr;
            int var4 = skuArr.length;

            for(int var5 = 0; var5 < var4; ++var5) {
                String s = var3[var5];
                sb.append(s).append(splitor);
            }

            return sb.length() < 1 ? sb.toString() : sb.substring(0, sb.length() - 1);
        }
    }

    public static int string2int(String str, int defaultVal) {
        try {
            return Integer.parseInt(str);
        } catch (NumberFormatException var3) {
            return defaultVal;
        }
    }

    public static BigDecimal string2bigDecimal(String str) {
        try {
            return new BigDecimal(str);
        } catch (NumberFormatException var2) {
            return null;
        }
    }

    public static boolean in(String s, String... in) {
        String[] var2 = in;
        int var3 = in.length;

        for(int var4 = 0; var4 < var3; ++var4) {
            String i = var2[var4];
            if (i != null && i.equals(s)) {
                return true;
            }
        }

        return false;
    }

    public static boolean notIn(String s, String... in) {
        return !in(s, in);
    }

    public static boolean isBetween(int num, int low, int high) {
        return num >= low && num <= high;
    }

    public static long string2long(String str, long defaultVal) {
        try {
            return Long.parseLong(str);
        } catch (NumberFormatException var4) {
            return defaultVal;
        }
    }

    public static boolean isLetterDigit(String str) {
        boolean result = false;

        try {
            String regex = "^[a-z0-9A-Z]+$";
            result = str.matches(regex);
            return result;
        } catch (Exception var3) {
            return result;
        }
    }

    public static boolean equals(Object a, Object b) {
        if (a == b) {
            return true;
        } else {
            return a != null && b != null ? a.equals(b) : false;
        }
    }

    public static String getRandomTraceId() {
        return RandomStringUtils.randomAlphanumeric(16);
    }

    public static String getRandomTraceId(int len) {
        return len > 0 ? RandomStringUtils.randomAlphanumeric(len) : "";
    }

    public static String num2Chinese(int section) {
        if (section >= 10 && section < 20) {
            return "十" + num2Chinese(section % 10);
        } else {
            StringBuilder chnStr = new StringBuilder();
            StringBuilder strIns = new StringBuilder();
            int unitPos = 0;

            for(boolean zero = true; section > 0; section = (int)Math.floor((double)((float)section / 10.0F))) {
                int v = section % 10;
                if (v == 0) {
                    if (!zero) {
                        zero = true;
                        chnStr.append(CHINESE_NUM_CHAR[v]).append(chnStr);
                    }
                } else {
                    zero = false;
                    strIns.delete(0, strIns.length());
                    strIns.append(CHINESE_NUM_CHAR[v]);
                    strIns.append(CHINESE_UNIT_CHAR[unitPos]);
                    chnStr.insert(0, strIns);
                }

                ++unitPos;
            }

            return chnStr.toString();
        }
    }

    public static String getHashCode(String formula) {
        return Hashing.sha256().hashBytes(formula.getBytes()).toString();
    }

    public static long date2TimeStamp(String date_str, String format) {
        try {
            SimpleDateFormat sdf = new SimpleDateFormat(format);
            return sdf.parse(date_str).getTime();
        } catch (Exception var3) {
            var3.printStackTrace();
            return 0L;
        }
    }

    public static long date2TimeStamp(String date_str) {
        try {
            SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
            return sdf.parse(date_str).getTime();
        } catch (Exception var2) {
            var2.printStackTrace();
            return 0L;
        }
    }

    public static <T> List<List<T>> fixedGrouping(List<T> source, int n) {
        if (null != source && source.size() != 0 && n > 0) {
            int sourceSize = source.size();
            int size = source.size() / n + 1;
            List<List<T>> result = new ArrayList(size);

            for(int i = 0; i < size; ++i) {
                List<T> subset = new ArrayList();

                for(int j = i * n; j < (i + 1) * n; ++j) {
                    if (j < sourceSize) {
                        subset.add(source.get(j));
                    }
                }

                result.add(subset);
            }

            return result;
        } else {
            return Collections.emptyList();
        }
    }

    public static long date2TimeStamp(Date date) {
        try {
            return date.getTime();
        } catch (Exception var2) {
            var2.printStackTrace();
            return 0L;
        }
    }

    public static String dividePercent(BigDecimal a, BigDecimal b, Integer c) {
        String val = "-";
        if (a != null && b != null) {
            if (b.compareTo(BigDecimal.ZERO) == 0) {
                return val;
            } else {
                val = a.multiply(new BigDecimal("100")).divide(b, c, 5).toString();
                return val + "%";
            }
        } else {
            return val;
        }
    }

}